package mdm.led.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;
import mdm.led.entity.InspectorList;
import mdm.led.entity.User;
import mdm.led.entity.four.FourDatilsListRequest;
import mdm.led.entity.four.FourDatilsParam;
import mdm.led.entity.four.FourDetails;
import mdm.led.entity.four.FourInspector;
import mdm.led.entity.four.FourInspectorRequest;
import mdm.led.service.DopeService;
import mdm.led.service.FourInspectorService;

/**
 *
 * @author :Monarch丿He
 * @date :2018年11月2日
 * @explain :督查四必 WEB 控制器
 */
@Controller
@RequestMapping("fourInspectorWeb")
@Api(description = "督查四必 WEB")
public class FourInspectorWebController {

	private static final Logger LOGGER = LoggerFactory.getLogger(FourInspectorWebController.class);

	@Autowired
	private DopeService dopeserviceImpl;
	@Autowired
	private FourInspectorService fourInspectorServiceImpl;

	@ResponseBody
	@ApiOperation("添加督查表")
	@RequestMapping(value = "addFourInspector", method = RequestMethod.POST)
	public AjaxResponse addFourInspector(@RequestBody FourInspector fourInspector) {

		LOGGER.info("\n---------督查角色添加流程表格----------");
		Boolean iRet = fourInspectorServiceImpl.addFourInspector(fourInspector);
		LOGGER.info("\n-----响应值:" + iRet + "-------");
		if (iRet) {
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "添加成功");
		} else {
			return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, "添加失败");
		}

	}

	@ApiOperation("绑定发送对象下拉")
	@ResponseBody
	@RequestMapping(value = "bindObjectSelect", method = RequestMethod.GET)
	public AjaxResponse bindObjectSelect() {
		LOGGER.info("\n-------------- 绑定发送对象下拉 ------------");
		// 一层键为 部门类型值为 二层为键为用户ID 值为用户名
		HashMap<String, Map<String, Object>> hashMap = new HashMap<>();
		// 四个集合分别保存 四个类型
		// 支队
		Map<String, Object> moleculeArmy = new HashMap<>();
		// 科室
		Map<String, Object> subjectRoom = new HashMap<>();
		// 大队
		Map<String, Object> bigArmy = new HashMap<>();
		// 中队
		Map<String, Object> centreArmy = new HashMap<>();
		// 所有用户
		List<User> list = dopeserviceImpl.findAllDoUser();
		for (User user : list) {
			// 为督查角色时 忽略
			if (user.getRoleId() == 3 || user.getUsertype() == 2) {
				continue;
			}
			switch (user.getDepartmentType()) {
			// 支队
			case 1:
				moleculeArmy.put(user.getcUserId().toString(), user.getcNickname());
				break;
			// 科室
			case 2:
				subjectRoom.put(user.getcUserId().toString(), user.getcNickname());
				break;
			// 大队
			case 3:
				bigArmy.put(user.getcUserId().toString(), user.getcNickname());
				break;
			// 中队
			case 4:
				centreArmy.put(user.getcUserId().toString(), user.getcNickname());
				break;

			}
		}
		hashMap.put("moleculeArmy", moleculeArmy);
		hashMap.put("subjectRoom", subjectRoom);
		hashMap.put("bigArmy", bigArmy);
		hashMap.put("centreArmy", centreArmy);
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(hashMap) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, hashMap);
	}

	@ApiOperation("查询督查四必栏目列表")
	@ResponseBody
	@RequestMapping(value = "findInspectorList", method = RequestMethod.GET)
	public AjaxResponse findInspectorList(@RequestParam(value = "name", required = false) String name,
			@RequestParam("pageSize") Integer pageSize, @RequestParam("currPage") Integer currPage) {
		LOGGER.info("\n-------------- 查询督查四必栏目列表 ------------");

		// 开启分页
		Page<InspectorList> page = PageHelper.startPage(currPage, pageSize);
		// 需要分页的数据集合
		List<InspectorList> inspectorList = fourInspectorServiceImpl.findInspectorList(name);
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		// 数据
		data.put("inspectorList", inspectorList);
		// 当前页
		data.put("currPage", page.getPageNum());
		// 页总数
		data.put("pageCount", page.getPages());
		// 页大小
		data.put("pageSize", page.getPageSize());
		// 总条数
		data.put("totalCount", page.getTotal());

		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(data) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
	}

	@ApiOperation(value = "根据部门查询指定类型督查详情列表", notes = "传参:{}")
	@RequestMapping(value = "findDetails", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResponse findDetails(@RequestBody FourDatilsListRequest request,
			@RequestParam("pageSize") Integer pageSize, @RequestParam("currPage") Integer currPage) {
		LOGGER.info("\n--------------- 督查四必详情列表 ---------------");
		LOGGER.info("请求参数:{" + JSON.toJSONString(request) + "}");
		LOGGER.info("\n--------------- pageSize:" + pageSize + " ---------------");
		LOGGER.info("\n--------------- currPage:" + currPage + " ---------------");
		FourDatilsParam param = new FourDatilsParam();
		param.setEstate(request.getEstate());
		param.setiRet(1);
		param.setSectionId(request.getDepartmentId());
		param.setName(request.getUsername());

		// 开启分页
		Page<FourDetails> page = PageHelper.startPage(currPage, pageSize);
		List<FourDetails> detailss = fourInspectorServiceImpl.findDetailsWeb(param);
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		// 数据
		data.put("inspectorList", detailss);
		// 当前页
		data.put("currPage", page.getPageNum());
		// 页总数
		data.put("pageCount", page.getPages());
		// 页大小
		data.put("pageSize", page.getPageSize());
		// 总条数
		data.put("totalCount", page.getTotal());
		LOGGER.info("\n--------------- 结果:" + detailss + " ---------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
	}

	@ApiOperation(value = "根据 督查详情表ID 查询 督查详情表", notes = "需要传入详情表ID")
	@ResponseBody
	@RequestMapping(value = "findDetailsById", method = RequestMethod.GET)
	public AjaxResponse findDetailsById(@RequestParam("detailsId") Integer id) {

		LOGGER.info("\n--------------- 督查四必详情列表 ---------------");

		FourDetails details = fourInspectorServiceImpl.findDetailsById(id);
		LOGGER.info("\n--------------- 结果:" + details + " ---------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, details);
	}

	@ApiOperation(value = "查询所有可用的督查表列表")
	@ResponseBody
	@RequestMapping(value = "findAllFourInspector", method = RequestMethod.GET)
	public AjaxResponse findAllFourInspector(@RequestParam(value = "name", required = false) String name,
			@RequestParam("pageSize") Integer pageSize, @RequestParam("currPage") Integer currPage) {
		LOGGER.info("\n--------------- 查询所有可用的督查表列表 ---------------");
		LOGGER.info("\n--------------- name:" + name + " ---------------");
		LOGGER.info("\n--------------- pageSize:" + pageSize + " ---------------");
		LOGGER.info("\n--------------- currPage:" + currPage + " ---------------");
		// 开启分页
		Page<InspectorList> page = PageHelper.startPage(currPage, pageSize);
		// 需要分页的数据集合
		List<FourInspectorRequest> response = fourInspectorServiceImpl.findAllFourInspector(name);
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		// 数据
		data.put("inspectorList", response);
		// 当前页
		data.put("currPage", page.getPageNum());
		// 页总数
		data.put("pageCount", page.getPages());
		// 页大小
		data.put("pageSize", page.getPageSize());
		// 总条数
		data.put("totalCount", page.getTotal());
		LOGGER.info("\n--------------- 结果:" + JSON.toJSONString(data) + " ---------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);
	}

	@ApiOperation(value = "根据督查表id查询督查表详情信息", notes = "传入督查表id")
	@ResponseBody
	@RequestMapping(value = "findFourInspector", method = RequestMethod.GET)
	public AjaxResponse findFourInspector(@RequestParam("inspectorId") Integer id) {
		LOGGER.info("\n-----------根据督查表id查询督查表详情信息------------");
		LOGGER.info("\n-----------ID:" + id + "------------");
		FourInspectorRequest response = fourInspectorServiceImpl.findFourInspector(id);
		LOGGER.info("\n--------------- 结果:" + JSON.toJSONString(response) + " ---------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, response);
	}

	@ApiOperation(value = "修改表状态", notes = "传入督查表id")
	@ResponseBody
	@RequestMapping(value = "updateFourInspectorStatus", method = RequestMethod.PUT)
	public AjaxResponse updateFourInspectorStatus(@RequestParam("inspectorId") Integer id) {
		LOGGER.info("\n-----------修改表状态------------");
		LOGGER.info("\n-----------ID:" + id + "------------");
		int iRet = fourInspectorServiceImpl.updateFourInspectorStatus(id);
		String isSuccess = "";
		isSuccess = (iRet > 0) ? (isSuccess = "成功") : (isSuccess = "失败");
		LOGGER.info("\n--------------- 结果:" + isSuccess + " ---------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, "修改成功!");
	}

}
